package com.zhuss.idea.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.idea.idea.CreateIdeaBo;
import com.zhuss.idea.db.dao.ext.IdeaDao;
import com.zhuss.idea.db.entity.Idea;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.processor.UserRead;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;

/**
 * 创建奇思妙想 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("ideaCreate")
public class IdeaCreate extends AbstractProcessor<Object, CreateIdeaBo> implements Processor<Object, CreateIdeaBo> {

    @Autowired
    private IdeaDao ideaDao;
    
    @Autowired
	@Qualifier("ideaRead")
	private IdeaRead ideaRead;
    
    @Autowired
    @Qualifier("userRead")
    private UserRead userRead;
    
    
    /**
     * 校验
     */
    @Override
    public void check(CreateIdeaBo bo) throws WpsException {
    	String title = bo.getTitle();
    	if (title == null || "".equals(title.trim())) {
    		throw new WpsException(ExceptionCode.奇思妙想标题为空);
    	}
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(CreateIdeaBo param) throws WpsException {
    	Idea record = new Idea();
    	record.setTitle(param.getTitle());
    	record.setContent(param.getContent());
    	record.setPic(param.getPic());
    	record.setSort(param.getSort());
    	
    	record.setCreatorUrl(userRead.picUrl(param.getConcurrentUserId()));
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	ideaDao.create(record);
    	param.setTargetId(record.getIdeaId());
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CreateIdeaBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.奇思妙想.key());
    	record.setAction(LogActionEnum.奇思妙想创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getTitle());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
